/* 
 * File:   main.c
 * Author: hp
 *
 * Created on 2011年3月11日, 上午9:30
 */

#include <stdio.h>
#include <stdlib.h>
#include "header.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>


extern void run(void * connfd);
extern void fetch();

/*
 * 
 */
int main(int argc, char** argv) {
    int listenfd, connfd;
    struct sockaddr_in servaddr;
    struct sockaddr_in clntaddr;


    int n;
    int i;
    long int size=0;

    /*Initialize */

    init();

    head=NULL;
    cache_left=cache_size;

    pthread_mutex_init(&mutex_head,NULL);
    pthread_mutex_init(&mutex_cache_left,NULL);

    /*END*/

    listenfd=socket(AF_INET,SOCK_STREAM,0);
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    //servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
    if (inet_aton(ip_addr,&(servaddr.sin_addr.s_addr))==0)
    {
        perror("inet_aton");
        exit(EXIT_FAILURE);
    }

    servaddr.sin_port=htons(serv_port);

    bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
    listen(listenfd,LISTENQ);
    

    while(1){
        connfd=accept(listenfd,NULL,NULL);
        pthread_t thread;
        pthread_create(&thread,NULL,run,(void*)connfd);
        //run(connfd);
    }


    return (EXIT_SUCCESS);
}

